package com.control;

import com.dao.StudentDao;
import com.model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * @author :wn
 * @program : 项目五登录界面
 * @descript :
 * @create :2020-11-06 23:37*/


@WebServlet(value = "/view")
public class ViewControl extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ArrayList<Student> list=new ArrayList();
            ResultSet rs=new StudentDao().quaryMethod();
            while (rs.next()){
                Student student=new Student();
                student.setName(rs.getString("name"));
                student.setAge(rs.getString("age"));
                student.setSex(rs.getString("sex"));
                student.setDept(rs.getString("sdept"));
                list.add(student);
            }
            //关闭连接，不能在quaryMethod方法中去关闭连接；会报Operation not allowed after ResultSet closed 异常；
            // 根据Resultset的getstatment()
            //statement的getConnection();来获取connection对象；再进行关闭
            // 来关闭连接；不关闭的话，
            // 会出现数据库连接池资源不够用（同昨天晚上的错误，忘记关闭连接）
           Statement st= rs.getStatement();
            Connection con=st.getConnection();
            con.close();
            st.close();
            rs.close();
            HttpSession session=req.getSession();
            session.setAttribute("studentList",list);
            req.getRequestDispatcher("/viewInterface.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
